// 引入 QCloud 小程序增强 SDK
var qcloud = require('../../../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};
var showMsg = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
//获取应用实例
var app = getApp()
Page({
  data: {
  "videoSrc":'',
  "busy":false,
  "busy1":false,
  "textarea":false,
  "upload_busy":false
  },
  onLoad: function () {
    console.log('onLoad');
    app.getUserInfo(function(userInfo){
      console.log(userInfo);
    });
    this.chooseVideo();
  },
  onShow: function () {
    console.log('onShow');
  },
  chooseVideo: function() {
    var self = this;
    if(self.data.upload_busy){
      return;
    }
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      success: function(res) {
        console.log('chooseVideo success, temp path is', res.tempFilePath)
        console.log(res);
        if(res.size>50*1024*1000){
          showMsg('提示信息','视频大小不能大于50M');
          return;
        }
        var videoSrc = res.tempFilePath;
        self.setData({
            'upload_busy':true,
            'videoSrc':""
        })
        //上传图片到服务器
        wx.uploadFile({
          url: config.service.uploadUrl,
          filePath: videoSrc,
          name: 'file',
          success: function(res) {
            if(res.statusCode==200){
              var data=JSON.parse(res.data);
              console.log(data);
              if(data.success==true){
                self.setData({
                    'videoSrc':data.file.url,
                    'videoContext':wx.createVideoContext('myVideo')
                })
                showSuccess('上传成功');
              }else{
                showMsg('提示信息','上传失败');
              }
            }else if(res.statusCode==413){
              showMsg('提示信息','上传失败,文件过大');
            }else{
              showMsg('提示信息','上传失败');
            }
          },
          fail: function({errMsg}) {
            console.log('uploadVideo fail, errMsg is', errMsg);
            showMsg('提示信息','上传失败,'+errMsg);
          },
          complete:function(){
            self.setData({
                'upload_busy':false
            })
          }
        })
      },
      fail: function({errMsg}) {
        console.log('chooseVideo fail, err is', errMsg)
      }
    })
  },
  formSubmit: function(e) {
    // console.log(e);
    
    var self = this;
    if(self.data.upload_busy||self.data.busy1){
      return;
    }
    if(self.data.videoSrc==""){
      showMsg("提示信息","请先上传视频");
      return;
    }
    var data={};
    if(self.data.videoSrc!=""){
      data.image_url=self.data.videoSrc;
    }
    if(e.detail.formId!='the formId is a mock one'){
      data.form_id=e.detail.formId;
    }
    data.type=3;
    data.expire_in=1440;
    self.setData({
        'busy1':true
    })
    qcloud.request({
            // 要请求的地址
            url: config.service.channelMessageAddUrl,
            method:"POST",
            data: data,
            success(result) {
              console.log(result);
              if(result.data.success==true){
                 showSuccess("成功");
                 self.data.videoContext.pause();
                wx.switchTab({
                  url: '/pages/channel/index/index'
                })
              }else{
                showMsg("提示信息","新建消息失败，"+result.data.message);
              }
            },

            fail(error) {
                console.log('request fail', error);
                showMsg("提示信息","新建消息失败");
            },

            complete() {
                self.setData({
                      'busy1':false
                  })
                console.log('request complete');
            }
      });
  }
})